<?php
// $Id: template.php,v 1.1.2.7.2.17.2.43.2.3 2010/11/25 16:34:27 adrinux Exp $

// Auto-rebuild the theme registry during theme development.
if (theme_get_setting('clean_rebuild_registry')) {
  drupal_rebuild_theme_registry();
}

/**
 * Implementation of hook_theme().
 */
// function clean_theme($existing, $type, $theme, $path) {
  // global $base_path;
  // Compute the conditional stylesheets.
  // if (!module_exists('conditional_styles')) {
  //   //include_once $base_path . drupal_get_path('theme', 'clean') . '/template.conditional-styles.inc';
  //   // _conditional_styles_theme() only needs to be run once.
  //   if ($theme == 'clean') {
  //     //_conditional_styles_theme($existing, $type, $theme, $path);
  //   }
  // }
  
  // return array(
  //   'fieldset' => array(
  //     'variables' => array(
  //       'arguments' => array(
  //         'element' => array(),
  //       ),
  //       'template' => 'fieldset',
  //     ),
  //   ),
  // );
// }

function clean_preprocess_html(&$variables) {
  $base_path = base_path();
  $variables['base_theme'] = $path_to_clean = drupal_get_path('theme', 'clean') . '/';
  $path_to_theme = path_to_theme() . '/';
  global $user, $theme_key;

  clean_body_classes($variables);
  clean_html_attributes($variables);

  $settings = array(
    'theme_path' => $base_path . $path_to_clean,
    'theme_key' => str_replace('_', '-', $theme_key),
    'columns' => theme_get_setting('clean_960gs_columns'),
    'default_state' => theme_get_setting('clean_960gs_default_state'),
  );

  if (theme_get_setting('clean_enable_960_grid')) {
    drupal_add_js(array('clean' => $settings), 'setting');
    drupal_add_js($path_to_clean . 'js/grid.js', 'theme', 'header', FALSE, TRUE, FALSE);
    drupal_add_css($path_to_clean . 'css/grid.css', 'theme');
    $variables['scripts'] = drupal_get_js();
  }

  //$variables['styles'] = drupal_get_css(clean_css_stripped());
  
  // Add conditional stylesheets.
  // if (!module_exists('conditional_styles')) {
  //   $variables['styles'] .= $variables['conditional_styles'] = variable_get('conditional_styles_' . $GLOBALS['theme'], '');
  // }
}

function clean_process_html(&$variables) {
  
}

/**
 * Override or insert variables into the node templates.
 *
 * @param $variables
 *   An array of variables to pass to the theme template.
 */
function clean_preprocess_node(&$variables) {
  global $theme_key;
  $node = $variables['node'];

  $variables['pre'] = '';
  $variables['post'] = '';

  if ($variables['title']) {
    $variables['title'] = l($variables['title'], "node/{$variables['node']->nid}", array('html' => TRUE));
  }

  // Create node classes.
  clean_node_classes($variables);

  // Add node template file suggestions for node-type-teaser and node-type-prevew
  if (!$variables['page']) {
    $variables['template_files'][] = "node-$node->type--teaser";
    $variables['template_files'][] = "node-$node->type-$node->nid--teaser";
  }

  if ($node->op == 'Preview' && !$variables['teaser']) {
    $variables['template_files'][] = "node-$node->type--preview";
    $variables['template_files'][] = "node-$node->type-$node->nid--preview";
  }

  $function = "${theme_key}_preprocess_node_$node->type";
  if (function_exists($function)) {
    call_user_func_array($function, array(&$variables));
  }
}

/**
 * Override or insert variables into the page templates.
 *
 * @param $variables
 *   An array of variables to pass to the theme template.
 */
function clean_preprocess_page(&$variables) {
  $base_path = base_path();
  $variables['base_theme'] = $path_to_clean = drupal_get_path('theme', 'clean') . '/';
  $path_to_theme = path_to_theme() . '/';
  global $user, $theme_key;

  $variables['path'] = $base_path . $path_to_theme;

  $site_name = filter_xss_admin(variable_get('site_name', 'Drupal'));
  $variables['site_name_themed'] = theme_get_setting('toggle_name') ? l($site_name, '<front>', array('attributes' => array('id' => array('site-name')))) : '';

  $variables['site_slogan_themed'] = ($variables['site_slogan']) ? '<span id="site-slogan">'. $variables['site_slogan'] .'</span>' : '';

  if ($variables['logo']) {
    $logo_path = preg_replace('@^'. $base_path .'@', '', $variables['logo']);
    file_exists($logo_path) ?
      $image = theme('image', array('path' => $logo_path, 'alt' => $site_name)) :
      $image = theme('image', array('path' => $path_to_clean .'/logo.png', 'alt' => $site_name));
    $variables['logo_themed'] = l($image, '<front>', array('attributes' => array('id' => 'logo', 'rel' => 'home', 'title' => t('Return to the !site_name home page', array('!site_name' => $site_name))), 'html' => TRUE));
  }

  if (!empty($variables['main_menu'])) {
    // Don't destroy menu links.
    $variables['main_menu_links'] = $variables['main_menu'];
    $variables['main_menu'] = array(
      '#theme' => 'links__system_main_menu',
      '#links' => $variables['main_menu'],
    );
    $variables['main_menu']['#attributes']['class'][] = 'main-menu';
    $variables['main_menu']['#attributes']['class'][] = 'links';
  }

  if (!empty($variables['secondary_menu'])) {
    // Don't destroy menu links.
    $variables['secondary_menu_links'] = $variables['secondary_menu'];
    $variables['secondary_menu'] = array(
      '#theme' => 'links__system_secondary_menu',
      '#links' => $variables['secondary_menu'],
    );
    $variables['secondary_menu']['#attributes']['class'][] = 'secondary-menu';
    $variables['secondary_menu']['#attributes']['class'][] = 'links';
  }
}

function clean_html_attributes(&$variables) {
  $html_attributes = array();
  $language = $variables['language'];

  $html_attributes['xmlns'] = 'http://www.w3.org/1999/xhtml';
  $html_attributes['xml:lang'] = $language->language;
  $html_attributes['lang'] = $language->language;
  $html_attributes['dir'] = $language->dir;

  $variables['html_attributes'] = $html_attributes;
}

/**
 * Create node classes for node templates files.
 *
 * @param $variables
 *   An array of variables to pass to the theme template.
 * @return $classes
 *   A string of node classes for inserting into the node template.
 */
function clean_node_classes(&$variables) {
  $node = $variables['node'];
  $classes = array();

  // Merge in existing classes.
  if ($variables['classes']) {
    $classes = array($variables['classes']);
  }

  $classes[] = 'node';
  $classes[] = 'node-'. $node->type;
  if ($variables['page']) {
    $classes[] = 'node-'. $node->type . '-page';
  }
  elseif ($variables['teaser']) {
    $classes[] = 'node-teaser';
    $classes[] = 'node-'. $node->type . '-teaser';
  }
  if ($variables['sticky']) {
    $classes[] = 'sticky';
  }
  if (!$variables['status']) {
    $classes[] = 'node-unpublished';
  }
  $classes[] = 'clearfix';

  $variables['attributes_array']['id'] = 'node-'. $node->nid;
  $variables['attributes_array']['class'] = implode(' ', $classes);
}

/**
 * Create body classes for page templates files in addition to those provided by core.
 *
 * @param $variables
 *   An array of variables to pass to the theme template.
 * @return
 *   Adds data to $variables directly.
 */
function clean_body_classes(&$variables) {
  $classes = array();

  // Merge in existing classes.
  if (!empty($variables['classes_array'])) {
    $classes = array($variables['classes_array']);
  }

  // Create classes for the user-visible path sections.
  global $base_path;
  list(,$path) = explode($base_path, request_uri(), 2);
  list($path,) = explode('?', $path, 2);
  $path = rtrim($path, '/');

  // Create section classes down to 3 levels. Path is empty if we're at <front>.
  if ($path) {
    $path_alias_sections = array_slice(explode('/', $path), 0, 3);
    $section_path = 'section';
    foreach ($path_alias_sections as $arg_piece) {
      $section_path .= '-'. $arg_piece;
      $classes[] = $section_path;
    }
  }

  if ($variables['is_admin']) {
    $classes[] = 'admin-section';
  }

  $variables['attributes_array']['class'][] = $classes;

  // System path gives us the id, replacing slashes with dashes.
  $system_path = drupal_get_normal_path($path);
  if (!empty($section_path)) {
    $variables['attributes_array']['id'] = 'page-'. str_replace('/', '-', $system_path);
  }
}

/**
 * Implementation of preprocess_block().
 */
function clean_preprocess_block(&$variables) {
  $block = $variables['block'];
  
  $variables['attributes_array']['id'] = $variables['block_html_id'];
  $variables['attributes_array']['class'] = $variables['classes_array'];
  $variables['title_attributes_array']['class'][] = 'block-title';
  $variables['content_attributes_array']['class'][] = 'block-content';
  $variables['content_attributes_array']['class'][] = 'clearfix';
  
  $variables['pre'] = '';
  $variables['title'] = $block->subject;
  $variables['post'] = '';
  
  $variables['hook'] = 'block';
}

/**
 * Override or insert variables into the comment templates.
 *
 * @param $variables
 *   An array of variables to pass to the theme template.
 */
function clean_preprocess_comment(&$variables) {
  $comment = $variables['comment'];

  $variables['pre'] = '';
  $variables['post'] = '';
  $variables['attributes_array']['id'] = "comment-$comment->cid";
  $variables['attributes_array']['class'] = "comment {$variables['status']}";

  if ($comment->new) {
    $variables['attributes_array']['class'] .= ' comment-new';
  }

  if ($variables['zebra']) {
    $variables['attributes_array']['class'] .= " comment-{$variables['zebra']}";
  }
}

/**
 * Implementation of preprocess_box().
 */
function clean_preprocess_box(&$variables) {
  $variables['pre'] = '';
  $variables['post'] = '';
  $variables['attributes_array']['class'] = 'box';
}

/**
 * Implementation of preprocess_fieldset().
 */
function clean_preprocess_fieldset(&$variables) {
  // $variables['hook'] = 'fieldset';
  // $element = $variables['element'];
  // 
  // $variables['pre'] = '';
  // $variables['post'] = '';
  // 
  // $attributes_array = array();
  // if (isset($element['#attributes'])) {
  //   $attributes_array = $element['#attributes'];
  //   $class = $attributes_array['class'];
  // }
  // 
  // $attributes_array['class'] = 'fieldset';
  // $attributes_array['class'] .= ' '. $class;
  // if (!empty($element['#collapsible']) || !empty($element['#collapsed'])) {
  //   $attributes_array['class'] .= ' collapsible';
  // }
  // if (!empty($element['#collapsed'])) {
  //   $attributes_array['class'] .= ' collapsed';
  // }
  // 
  // $variables['attributes_array'] = $attributes_array;
  // 
  // if (!empty($element['#description'])) {
  //   $description = "<div class='description'>{$element['#description']}</div>";
  // }
  // if (!empty($element['#children'])) {
  //   $children = $element['#children'];
  // }
  // if (!empty($element['#value'])) {
  //   $value = $element['#value'];
  // }
  // $variables['content'] = $description . $children . $value;
  // 
  // if (!empty($element['#title'])) {
  //   $variables['title'] = $element['#title'];
  // }
  // if (!empty($element['#collapsible']) || !empty($element['#collapsed'])) {
  //   $variables['title'] = l($variables['title'], $_GET['q'], array('fragment' => 'fieldset'));
  // }
}

/**
 * Override theme_menu_local_tasks().
 */
function clean_menu_local_tasks() {
  $output = array();

  if ($primary = menu_primary_local_tasks()) {
    $primary['#prefix'] = '<ul class="tabs primary links clearfix">';
    $primary['#suffix'] = '</ul>';
    $output[] = $primary;
  }
  if ($secondary = menu_secondary_local_tasks()) {
    $secondary['#prefix'] = '<ul class="tabs secondary links clearfix">';
    $secondary['#suffix'] = '</ul>';
    $output[] = $secondary;
  }

  return $output;
}

/**
 * Strips CSS files from a Drupal CSS array whose filenames start with
 * prefixes provided in the $match argument.
 */
function clean_css_stripped($match = array('modules/*'), $exceptions = NULL) {
  // Set default exceptions
  if (!is_array($exceptions)) {
    $exceptions = array(
      'modules/system/system.css',
      'modules/update/update.css',
      'modules/openid/openid.css',
      'modules/acquia/*',
    );
  }
  $css = drupal_add_css();
  $match = implode("\n", $match);
  $exceptions = implode("\n", $exceptions);
  foreach (array_keys($css['all']['module']) as $filename) {
    if (drupal_match_path($filename, $match) && !drupal_match_path($filename, $exceptions)) {
      unset($css['all']['module'][$filename]);
    }
  }

  // This servers to move the "all" CSS key to the front of the stack.
  // Mainly useful because modules register their CSS as 'all', while
  // Tao has a more media handling.
  ksort($css);
  return $css;
}

// /**
//  * Only show the breadcrumb trail if there are more items than just 'Home'.
//  */
// function clean_breadcrumb($variables) {
//   // if (count($breadcrumb) > 1) {
//   //   return '<div class="breadcrumb">'. implode('  ', $breadcrumb) .'</div>';
//   // }
// 
//   $breadcrumb = $variables['breadcrumb'];
// 
//   if (!empty($breadcrumb)) {
//     // Provide a navigational heading to give context for breadcrumb links to
//     // screen-reader users. Make the heading invisible with .element-invisible.
//     $output = '<h2 class="element-invisible">' . t('You are here') . '</h2>';
// 
//     $output .= '<div class="breadcrumb">' . implode(' &raquo; ', $breadcrumb) . '</div>';
//     return $output;
//   }
// }

/**
 * Override theme_textfield().
 */
 //done
function clean_textfield($variables) {
  $element = $variables['element'];
  if ($element['#size'] >= 15) {
    $element['#size'] = '';
    $element['#attributes']['class'][] = "fluid";
  }
  return theme_textfield(array('element' => $element));
}

function clean_file($element) {
  _form_set_class($element, array('form-file'));
  if ($element['#size'] >= 15) {
    $element['#size'] = '';
    $element['#attributes']['class'] = isset($element['#attributes']['class']) ? "{$element['#attributes']['class']} fluid" : "fluid";
  }
  return theme('form_element', $element, '<input type="file" name="'. $element['#name'] .'"'. ($element['#attributes'] ? ' '. drupal_attributes($element['#attributes']) : '') .' id="'. $element['#id'] .'" size="'. $element['#size'] ."\" />\n");
}


/**
 * Override theme_password().
 */
function clean_password($variables) {
  $element = $variables['element'];
  $element['#attributes']['type'] = 'password';

  element_set_attributes($element, array('id', 'name', 'value', 'size', 'maxlength'));
  _form_set_class($element, array('form-text'));

  if (isset($element['#attributes']['size'])) {
      if ($element['#attributes']['size'] >= 15) {
					$element['#attributes']['size'] = '';
					$element['#attributes']['class'][] = 'fluid';
      }
  }
  
  return '<input' . drupal_attributes($element['#attributes']) . ' />';
}

/**
 * Override theme_links().
 */
// DONE
function clean_links($variables) {
  $links = $variables['links'];
  $attributes = $variables['attributes'];
  $heading = $variables['heading'];
  global $language_url;
  $output = '';

  if (count($links) > 0) {
    $output = '';

    // Treat the heading first if it is present to prepend it to the
    // list of links.
    if (!empty($heading)) {
      if (is_string($heading)) {
        // Prepare the array that will be used when the passed heading
        // is a string.
        $heading = array(
          'text' => $heading,
          // Set the default level of the heading.
          'level' => 'h2',
        );
      }
      $output .= '<' . $heading['level'];
      if (!empty($heading['class'])) {
        $output .= drupal_attributes(array('class' => $heading['class']));
      }
      $output .= '>' . check_plain($heading['text']) . '</' . $heading['level'] . '>';
    }

    $output .= '<ul' . drupal_attributes($attributes) . '>';

    $num_links = count($links);
    $i = 1;

    foreach ($links as $key => $link) {
      $class = array($key);

      // Add first, last and active classes to the list of links to help out themers.
      if ($i == 1) {
        $class[] = 'first';
      }
      if ($i == $num_links) {
        $class[] = 'last';
      }
      if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))
          && (empty($link['language']) || $link['language']->language == $language_url->language)) {
        $class[] = 'active';
      }
      $output .= '<li' . drupal_attributes(array('class' => $class)) . '>';

      if (isset($link['href'])) {
        // Pass in $link as $options, they share the same keys.
        $output .= l($link['title'], $link['href'], $link);
      }
      elseif (!empty($link['title'])) {
        // Some links are actually not links, but we wrap these in <span> for adding title and class attributes.
        if (empty($link['html'])) {
          $link['title'] = check_plain($link['title']);
        }
        $span_attributes = '';
        if (isset($link['attributes'])) {
          $span_attributes = drupal_attributes($link['attributes']);
        }
        $output .= '<span' . $span_attributes . '>' . $link['title'] . '</span>';
      }

      $i++;
      $output .= "</li>";
    }

    $output .= '</ul>';
  }

  return $output;
}

// DONE
function clean_menu_local_task($variables) {
  $link = $variables['element']['#link'];
  $link_text = $link['title'];

  if (!empty($variables['element']['#active'])) {
    // Add text to indicate active tab for non-visual users.
    $active = '<span class="element-invisible">' . t('(active tab)') . '</span>';

    // If the link does not contain HTML already, check_plain() it now.
    // After we set 'html'=TRUE the link will not be sanitized by l().
    if (empty($link['localized_options']['html'])) {
      $link['title'] = check_plain($link['title']);
    }
    $link['localized_options']['html'] = TRUE;
    $link_text = t('!local-task-title!active', array('!local-task-title' => $link['title'], '!active' => $active));
  }

  return '<li' . (!empty($variables['element']['#active']) ? ' class="active"' : '') . '>' . l($link_text, $link['href'], $link['localized_options']) . "</li>";
}
